<?php
class UserAction extends Action{
	
    function _initialize() {
		
	}


    public function index(){
	  //print_r($this->user);
	    if($this->mid){
		$basicfile = M('user_basicfile');
	    $myschool = $basicfile->field('school')->where('uid='.$this->mid)->find();
		//echo $myschool['school'];
	    $list['data'] = $basicfile->field('uid, uname')->where("school='{$myschool[school]}'")->order('id DESC')->limit(20)->findAll();
		$list['title'] = '他们和您在同一个学校';
		}else{
		 
		$list['data'] = M('user_basicfile')->order('id DESC')->limit(20)->findAll();
		$list['title'] = '你可能认识他们';
		}
	   
		//print_r($list);
		$this->assign('list',$list);
	    $this->setTitle('搜索用户');
		$this->display();
	}
	//搜索用户 、网摊
	public function sc(){
	    if($_POST['k']){
		   $search_key   =  $this->__getSearchKey ();
		   //print_r( $search_key);
		   $key = $search_key['key'];
		   $type = $search_key['type'];
		
	     if($type=='user'){
		   
		   $user = M('user');
		   $count = $user->where("uname LIKE '%{$key}%'")->count();    //计算总数
		   $p = new Page ( $count, 3 );
		   $list=$user->field('uid, uname')->where("uname LIKE '%{$key}%'")->limit($p->firstRow.','.$p->listRows)->order('uid DESC')->findAll();
		   
		 //print_r($list);
		 }else{
		   
		   $user = M('user_basicfile');
		   $count = $user->where("shopname LIKE '%{$key}%'")->count();    //计算总数
		   $p = new Page ( $count, 3 );
		   $list=$user->field('uid, uname, shopname')->where("shopname LIKE '%{$key}%'")->limit($p->firstRow.','.$p->listRows)->order('uid DESC')->findAll();
		 }
		$page = $p->show ();
		$this->assign ( "page", $page );
		$this->assign('list',$list);
		$this->assign('key',$key);
		$this->setTitle('搜索用网摊');
		$this->display('list');
	  }  
		
	}
    private function __getSearchKey() {
		$keys = array();
		// 为使搜索条件在分页时也有效，将搜索条件记录到SESSION中
		if (isset ( $_REQUEST ['k'] ) && ! empty ( $_REQUEST ['k'] )) {
			if ($_GET ['k']) {
				$keys['key'] = html_entity_decode ( urldecode ( $_GET ['k'] ), ENT_QUOTES );
			} elseif ($_POST ['k']) {
				$keys['key'] = $_POST ['k'];
				$keys['type'] = $_POST ['t'];
			}
			// 关键字不能超过30个字符
			if (mb_strlen ( $keys['key'], 'UTF8' ) > 30)
				$keys['key'] = mb_substr ( $key, 0, 30, 'UTF8' );
			$_SESSION ['home_user_search_key'] = trim ( serialize ( $keys['key'] ));
		    $_SESSION ['home_user_search_type'] = trim ( serialize ( $keys['type'] ));
		} else if (is_numeric ( $_GET [C ( 'VAR_PAGE' )] )) {
			$keys['key'] =  unserialize ( $_SESSION ['home_user_search_key'] );
			$keys['type'] = unserialize ( $_SESSION ['home_user_search_type'] );
		
		} else {
			//unset($_SESSION['home_user_search_key']);
		}
		//echo unserialize ($_SESSION ['home_user_search_key']);
		return $keys ;
	}
}
?>